﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using QLDBHYT.BUS;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;

namespace QLDBHYT
{
    public partial class frmPhieuXuat : Form
    {
        private PhieunhapBUS phieunhapBUS;
        public frmPhieuXuat()
        {
            InitializeComponent();
        }

        private void frmPhieuxuat_Load(object sender, EventArgs e)
        {
            GetNhomPC();
            GetKy();
            phieunhapBUS = new PhieunhapBUS();
        }
        private void GetNhomPC()
        {
            NhomPCBUS nhomPCBUS = new NhomPCBUS();
            cboNhomCP.ClearItems();
            cboNhomCP.DefColWidth = 300;
            cboNhomCP.AllowSort = true;
            cboNhomCP.DataSource = nhomPCBUS.GetListNhomPC();
        }
        private void GetKy()
        {
            NhomPCBUS nhomPCBUS = new NhomPCBUS();
            cboKy.ClearItems();
            cboKy.DefColWidth = 100;
            cboKy.AllowSort = true;
            cboKy.DataSource = nhomPCBUS.GetListKy();
        }

        private void btnTimkiem_Click(object sender, EventArgs e)
        {
            VisibleColumn(false);
            List<PhieunhapVO> lstPhieuNhap;
            if (cboKy.SelectedValue == null)
            {
                MessageBox.Show("Nhập kỳ");
                return;
            }
            if (cboNhomCP.SelectedValue == null)
            {
                MessageBox.Show("Nhập nhóm CP");
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
           
            string condition = "";
            if (txtTenhieu.Text != "")
            {
                condition = condition + " and Ten_hieu like N'%" + txtTenhieu.Text + "%'";
            }
            if (txtMahieu.Text != "")
            {
                condition = condition + " and d.ma_hieu like '%" + txtMahieu.Text + "%'";
            }
           
            Button button = sender as Button;

            if (button.Name == "btnSosanh")
            {

                lstPhieuNhap = phieunhapBUS.GetListPhieuXuat(Convert.ToInt32(cboKy.Columns[1].Value), Convert.ToInt32(cboKy.Columns[2].Value), cboNhomCP.Columns[1].Value.ToString().Trim(),
                cboKy.Columns[4].Value.ToString().Trim(), cboKy.Columns[5].Value.ToString().Trim(), condition, false);
            }
            else
            {
                lstPhieuNhap = phieunhapBUS.GetListPhieuXuat(Convert.ToInt32(cboKy.Columns[1].Value), Convert.ToInt32(cboKy.Columns[2].Value), cboNhomCP.Columns[1].Value.ToString().Trim(),
                cboKy.Columns[4].Value.ToString().Trim(), cboKy.Columns[5].Value.ToString().Trim(), condition, true);
            }
            
            flex.Rows.Count = 1;
            int i = 1,j=0;
            flex.Tree.Column = 2;

            foreach (PhieunhapVO phieunhapVO in lstPhieuNhap)
            {

                if (flex.Rows[i - 1]["mahieu"] != null && flex.Rows[i - 1]["mahieu"].ToString() != phieunhapVO.Mahieu) //insert node moi
                {
                    flex.Rows.InsertNode(i, 1);
                    
                    C1.Win.C1FlexGrid.CellStyle rs = this.flex.Styles.Add("rowcolor");
                    rs.ForeColor = Color.Blue;
                    flex.Rows[i - 1].Style = rs;
                    flex.Rows[i]["STT"] = i;
                    flex.Rows[i]["mahieu"] = phieunhapVO.Mahieu;
                    flex.Rows[i]["Tenhieu"] = phieunhapVO.Tenhieu;
                    flex.Rows[i]["Dvt"] = phieunhapVO.Dvt;
                    flex.Rows[i]["hamluong"] = phieunhapVO.Hamluong;
                    flex.Rows[i]["Nuocsx"] = phieunhapVO.Nuocsx;
                    flex.Rows[i]["sophieu"] = phieunhapVO.Phieuxuat;
                    flex.Rows[i]["ngayxuat"] = phieunhapVO.Ngayxuat;
                    flex.Rows[i]["losanxuat"] = phieunhapVO.Losanxuat;
                    flex.Rows[i]["handung"] = phieunhapVO.Handung;
                    flex.Rows[i]["SL"] = phieunhapVO.Sl;
                    if (phieunhapVO.Slnhap != 0) { flex.Rows[i]["Slnhap"] = phieunhapVO.Slnhap; }
                    if (phieunhapVO.Sldauky != 0) { flex.Rows[i]["Sldauky"] = phieunhapVO.Sldauky; }
                    if (phieunhapVO.Gia != 0) { flex.Rows[i]["Gia"] = phieunhapVO.Gia; }
                    if (phieunhapVO.Gia_bhxh != 0) { flex.Rows[i]["Giabhxh"] = phieunhapVO.Gia_bhxh; }
                    flex.Rows[i]["Slcuoiky"] = phieunhapVO.Slnhap + phieunhapVO.Sldauky - phieunhapVO.Slxuat;
                    j = i;
                }
                else
                {
                    flex.Rows.InsertNode(i, 2);
                    if (phieunhapVO.Slxuat != 0) //phieu xuat
                    {
                        for (int k = j; k < i; k++)
                        {
                            if (Convert.ToDouble(flex.Rows[k]["sl"]) > 0)
                            {
                                if (phieunhapVO.Slxuat <= Convert.ToDouble(flex.Rows[k]["sl"]))
                                {
                                    flex.Rows[k]["sl"] = Convert.ToDouble(flex.Rows[k]["sl"]) - phieunhapVO.Slxuat;
                                    flex.Rows[i]["Slxuat"] = phieunhapVO.Slxuat;
                                    if (phieunhapVO.Sttnh == "1")
                                    {
                                        flex.Rows[i]["ngoaitru"] = phieunhapVO.Slxuat;
                                    }
                                    else
                                    {
                                        flex.Rows[i]["noitru"] = phieunhapVO.Slxuat;
                                    }
                                    flex.Rows[i]["losanxuat"] = flex.Rows[k]["losanxuat"];
                                    flex.Rows[i]["handung"] = flex.Rows[k]["handung"];
                                    flex.Rows[i]["STT"] = i;
                                    flex.Rows[i]["mahieu"] = phieunhapVO.Mahieu;
                                    if (phieunhapVO.Slnhap != 0) { flex.Rows[i]["Slnhap"] = phieunhapVO.Slnhap; }
                                    if (phieunhapVO.Sldauky != 0) { flex.Rows[i]["Sldauky"] = phieunhapVO.Sldauky; }
                                    flex.Rows[i]["Tenhieu"] = phieunhapVO.Tenhieu;
                                    flex.Rows[i]["Dvt"] = phieunhapVO.Dvt;
                                    flex.Rows[i]["hamluong"] = phieunhapVO.Hamluong;
                                    flex.Rows[i]["Nuocsx"] = phieunhapVO.Nuocsx;
                                    flex.Rows[i]["sophieu"] = phieunhapVO.Phieuxuat;
                                    flex.Rows[i]["ngayxuat"] = phieunhapVO.Ngayxuat;
                                    if (phieunhapVO.Gia != 0) { flex.Rows[i]["Gia"] = phieunhapVO.Gia; }
                                    if (phieunhapVO.Gia_bhxh != 0) { flex.Rows[i]["Giabhxh"] = phieunhapVO.Gia_bhxh; }
                                    flex.Rows[i]["Slcuoiky"] = Convert.ToInt32(flex.Rows[i - 1]["Slcuoiky"]) + phieunhapVO.Slnhap + phieunhapVO.Sldauky - phieunhapVO.Slxuat;
                                    break;
                                }
                                else
                                {

                                    flex.Rows[i]["Slxuat"] = flex.Rows[k]["sl"];
                                    phieunhapVO.Slxuat = phieunhapVO.Slxuat - Convert.ToInt32(flex.Rows[k]["sl"]);
                                    if (phieunhapVO.Sttnh == "1")
                                    {
                                        flex.Rows[i]["ngoaitru"] = flex.Rows[k]["sl"];
                                    }
                                    else
                                    {
                                        flex.Rows[i]["noitru"] = flex.Rows[k]["sl"];
                                    }
                                    flex.Rows[i]["losanxuat"] = flex.Rows[k]["losanxuat"];
                                    flex.Rows[i]["handung"] = flex.Rows[k]["handung"];
                                    flex.Rows[i]["STT"] = i;
                                    flex.Rows[i]["mahieu"] = phieunhapVO.Mahieu;
                                    if (phieunhapVO.Slnhap != 0) { flex.Rows[i]["Slnhap"] = phieunhapVO.Slnhap; }
                                    if (phieunhapVO.Sldauky != 0) { flex.Rows[i]["Sldauky"] = phieunhapVO.Sldauky; }
                                    flex.Rows[i]["Tenhieu"] = phieunhapVO.Tenhieu;
                                    flex.Rows[i]["Dvt"] = phieunhapVO.Dvt;
                                    flex.Rows[i]["hamluong"] = phieunhapVO.Hamluong;
                                    flex.Rows[i]["Nuocsx"] = phieunhapVO.Nuocsx;
                                    flex.Rows[i]["sophieu"] = phieunhapVO.Phieuxuat;
                                    flex.Rows[i]["ngayxuat"] = phieunhapVO.Ngayxuat;
                                    if (phieunhapVO.Gia != 0) { flex.Rows[i]["Gia"] = phieunhapVO.Gia; }
                                    if (phieunhapVO.Gia_bhxh != 0) { flex.Rows[i]["Giabhxh"] = phieunhapVO.Gia_bhxh; }
                                    flex.Rows[i]["Slcuoiky"] = Convert.ToInt32(flex.Rows[i - 1]["Slcuoiky"]) + phieunhapVO.Slnhap - Convert.ToInt32(flex.Rows[k]["sl"]);
                                    i++;
                                    flex.Rows.InsertNode(i, 2);
                                    flex.Rows[k]["sl"] = 0;
                                }
                            }
                        }
                    }
                    else // phieu nhap
                    {
                        flex.Rows[i]["STT"] = i;
                        flex.Rows[i]["mahieu"] = phieunhapVO.Mahieu;
                        if (phieunhapVO.Slnhap != 0) { flex.Rows[i]["Slnhap"] = phieunhapVO.Slnhap; }
                        if (phieunhapVO.Sldauky != 0) { flex.Rows[i]["Sldauky"] = phieunhapVO.Sldauky; }
                        flex.Rows[i]["Tenhieu"] = phieunhapVO.Tenhieu;
                        flex.Rows[i]["Dvt"] = phieunhapVO.Dvt;
                        flex.Rows[i]["hamluong"] = phieunhapVO.Hamluong;
                        flex.Rows[i]["Nuocsx"] = phieunhapVO.Nuocsx;
                        flex.Rows[i]["sophieu"] = phieunhapVO.Phieuxuat;
                        flex.Rows[i]["ngayxuat"] = phieunhapVO.Ngayxuat;
                        if (phieunhapVO.Gia != 0) { flex.Rows[i]["Gia"] = phieunhapVO.Gia; }
                        if (phieunhapVO.Gia_bhxh != 0) { flex.Rows[i]["Giabhxh"] = phieunhapVO.Gia_bhxh; }
                        flex.Rows[i]["losanxuat"] = phieunhapVO.Losanxuat;
                        flex.Rows[i]["handung"] = phieunhapVO.Handung;
                        flex.Rows[i]["SL"] = phieunhapVO.Sl;
                        flex.Rows[i]["Slcuoiky"] = Convert.ToInt32(flex.Rows[i - 1]["Slcuoiky"]) + phieunhapVO.Slnhap + phieunhapVO.Sldauky - phieunhapVO.Slxuat;
                                       
                    }
                    
                }
                
                i++;


            }
            Cursor.Current = Cursors.Default;
        }
       
        private void cmdTonghop_Click(object sender, EventArgs e)
        {
            VisibleColumn(false);
            List<PhieunhapVO> lstPhieuNhap;
            if (cboKy.SelectedValue == null)
            {
                MessageBox.Show("Nhập kỳ");
                return;
            }
            if (cboNhomCP.SelectedValue == null)
            {
                MessageBox.Show("Nhập nhóm CP");
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
            
            string condition = "";
            if (txtTenhieu.Text != "")
            {
                condition = condition + " and Ten_hieu like N'%" + txtTenhieu.Text + "%'";
            }
            if (txtMahieu.Text != "")
            {
                condition = condition + " and d.ma_hieu like '%" + txtMahieu.Text + "%'";
            }

            lstPhieuNhap = phieunhapBUS.GetListPhieuXuatTH(Convert.ToInt32(cboKy.Columns[1].Value),
                Convert.ToInt32(cboKy.Columns[2].Value), cboNhomCP.Columns[1].Value.ToString().Trim(),
                cboKy.Columns[4].Value.ToString().Trim(), cboKy.Columns[5].Value.ToString().Trim(), condition);

            flex.Rows.Count = 1;
            int i = 1;
            flex.Tree.Column = 2;
            foreach (PhieunhapVO phieunhapVO in lstPhieuNhap)
            {
                //flex.Rows.Count = flex.Rows.Count + 1;
                if (flex.Rows[i - 1]["mahieu"].ToString() != phieunhapVO.Mahieu) //insert node moi
                {
                    flex.Rows.InsertNode(i, 1);
                    flex.Rows[i]["Slcuoiky"] = phieunhapVO.Slnhap + phieunhapVO.Sldauky - phieunhapVO.Slxuat;
                    C1.Win.C1FlexGrid.CellStyle rs = this.flex.Styles.Add("rowcolor");
                    rs.ForeColor = Color.Blue;
                    flex.Rows[i - 1].Style = rs;
                }
                else
                {
                    flex.Rows.InsertNode(i, 2);
                    flex.Rows[i]["Slcuoiky"] = Convert.ToInt32(flex.Rows[i - 1]["Slcuoiky"]) + phieunhapVO.Slnhap - phieunhapVO.Slxuat;

                }
                flex.Rows[i]["STT"] = i;
                flex.Rows[i]["mahieu"] = phieunhapVO.Mahieu;
                if (phieunhapVO.Slxuat != 0) { flex.Rows[i]["Slxuat"] = phieunhapVO.Slxuat; }
                if (phieunhapVO.Slnhap != 0) { flex.Rows[i]["Slnhap"] = phieunhapVO.Slnhap; }
                if (phieunhapVO.Sldauky != 0) { flex.Rows[i]["Sldauky"] = phieunhapVO.Sldauky; }
                flex.Rows[i]["Tenhieu"] = phieunhapVO.Tenhieu;
                flex.Rows[i]["Dvt"] = phieunhapVO.Dvt;
                flex.Rows[i]["hamluong"] = phieunhapVO.Hamluong;
                flex.Rows[i]["Nuocsx"] = phieunhapVO.Nuocsx;
                flex.Rows[i]["ngayxuat"] = phieunhapVO.Ngayxuat;
                if (phieunhapVO.Gia != 0) { flex.Rows[i]["Gia"] = phieunhapVO.Gia; }
                if (phieunhapVO.Gia_bhxh != 0) { flex.Rows[i]["Giabhxh"] = phieunhapVO.Gia_bhxh; }
                i++;
            }
            C1.Win.C1FlexGrid.CellStyle rs1 = this.flex.Styles.Add("rowcolor");
            rs1.ForeColor = Color.Blue;
            flex.Rows[i - 1].Style = rs1;
            Cursor.Current = Cursors.Default;
        }

        private void btnTheoNgay_Click(object sender, EventArgs e)
        {
           
            List<PhieunhapVO> lstPhieuNhap;
            if (cboKy.SelectedValue == null)
            {
                MessageBox.Show("Nhập kỳ");
                return;
            }
            if (cboNhomCP.SelectedValue == null)
            {
                MessageBox.Show("Nhập nhóm CP");
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
           
            string condition = "";
            if (txtTenhieu.Text != "")
            {
                condition = condition + " and Ten_hieu like N'%" + txtTenhieu.Text + "%'";
            }
            if (txtMahieu.Text != "")
            {
                condition = condition + " and d.ma_hieu like '%" + txtMahieu.Text + "%'";
            }

            lstPhieuNhap = phieunhapBUS.GetListPhieuXuatTH(Convert.ToInt32(cboKy.Columns[1].Value),
                Convert.ToInt32(cboKy.Columns[2].Value), cboNhomCP.Columns[1].Value.ToString().Trim(),
                cboKy.Columns[4].Value.ToString().Trim(), cboKy.Columns[5].Value.ToString().Trim(), condition);

            flex.Rows.Count = 1;
            int i = 1;
            int maxcol=7;
            flex.Tree.Column = 2;
            foreach (PhieunhapVO phieunhapVO in lstPhieuNhap)
            {

                if (flex.Rows[i - 1]["mahieu"].ToString() != phieunhapVO.Mahieu) //insert node moi
                {
                   // maxcol = 8;
                    flex.Rows.Count = flex.Rows.Count + 1;
                    flex.Rows[i]["STT"] = i;
                    flex.Rows[i]["mahieu"] = phieunhapVO.Mahieu;
                    flex.Rows[i]["Tenhieu"] = phieunhapVO.Tenhieu;
                    flex.Rows[i]["Dvt"] = phieunhapVO.Dvt;
                    flex.Rows[i]["hamluong"] = phieunhapVO.Hamluong;
                    flex.Rows[i]["Nuocsx"] = phieunhapVO.Nuocsx;
                    if (phieunhapVO.Gia != 0) { flex.Rows[i]["Gia"] = phieunhapVO.Gia; }
                    if (phieunhapVO.Gia_bhxh != 0) { flex.Rows[i]["Giabhxh"] = phieunhapVO.Gia_bhxh; }
                    if (phieunhapVO.Slxuat != 0) { flex.Rows[i][GetCol(phieunhapVO.Ngayxuat.ToString(), ref maxcol)] = phieunhapVO.Slxuat; }
                     
                    i++;
                }
                else
                {
                    if (phieunhapVO.Slxuat != 0) { flex.Rows[i-1][GetCol(phieunhapVO.Ngayxuat.ToString(), ref maxcol)] = phieunhapVO.Slxuat; }
                }
            }
            SapXepCol(maxcol);
            Cursor.Current = Cursors.Default;
        }
        private void VisibleColumn(bool flag)
        {
            for (int i = 10; i < 41; i++)
            {
                flex.Cols[i].Visible = flag;
            }
        }
        private int GetCol(string ngay,ref int col)
        {
            int i;
            for (i = 8; i < col; i++)
            {
                if (flex.Cols[i].Caption == ngay)
                {
                    return i;
                }
            }
            col++;
            flex.Cols[col].Visible = true;
            flex.Cols[col].Caption = ngay;
            
            return col;
        }
        private void SapXepCol(int col)
        {
           
            for (int i = 8; i <= col; i++)
            {
                for (int j = i + 1; j <= col; j++)
                {
                    if (DateTime.Compare(Convert.ToDateTime(flex.Cols[i].Caption), Convert.ToDateTime(flex.Cols[j].Caption))>0)
                    {
                       
                        flex.Cols[j].Move(i);
                    }
                }
            }
        }

        private void btnThekho_Click(object sender, EventArgs e)
        {
            DataRow newRow = null;
            Dataset.dsBCNX ds = new Dataset.dsBCNX();
            Cursor.Current = Cursors.WaitCursor;        
            for(int i = 1;i <flex.Rows.Count ;i++)
            {
                newRow = ds.Tables[1].NewRow();
                newRow["ma_hieu"] = flex.Rows[i]["mahieu"];
                newRow["ten_hieu"] = flex.Rows[i]["Tenhieu"].ToString();
                newRow["Hamluong"] = flex.Rows[i]["hamluong"];
                newRow["DVT"] = flex.Rows[i]["DVT"];
                newRow["Dongia"] = Convert.ToDouble(flex.Rows[i]["Gia"]);
                newRow["Ngay"] = flex.Rows[i]["ngayxuat"];
                if (flex.Rows[i]["SLdauky"] != null && flex.Rows[i]["SLNhap"] != null)
                {
                    newRow["SLNhap"] = Convert.ToDouble(flex.Rows[i]["SLNhap"]) + Convert.ToDouble(flex.Rows[i]["SLdauky"]);
                }
                else if(flex.Rows[i]["SLdauky"] != null)
                {
                   newRow["SLNhap"] = Convert.ToDouble(flex.Rows[i]["SLdauky"]); 
                }
                else
                {
                    if (flex.Rows[i]["SLNhap"] != null) { newRow["SLNhap"] = Convert.ToDouble(flex.Rows[i]["SLNhap"]);}
                }
                if (flex.Rows[i]["SLXuat"] != null) { newRow["SLXuat"] = flex.Rows[i]["SLXuat"] ;}
                if (flex.Rows[i]["slcuoiky"] != null) { newRow["SLTon"] = flex.Rows[i]["slcuoiky"]; } 

                ds.Tables[1].Rows.Add(newRow);

            }
            Cursor.Current = Cursors.Default;
            Report f = new Report();
            ReportDocument cr = new ReportDocument();
            cr.Load("rptThekho.rpt");
            cr.SetDataSource(ds);
            f.crpView.ReportSource = cr;
            f.Show();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
      

      

       
    }
}
